package com.lazy.book.service.system.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lazy.book.mapper.system.SysOperLogMapper;
import com.lazy.book.pojo.system.pojo.dto.sysoperlog.SysOperLogCreateDTO;
import com.lazy.book.pojo.system.pojo.dto.sysoperlog.SysOperLogListDTO;
import com.lazy.book.pojo.system.pojo.dto.sysoperlog.SysOperLogUpdateDTO;
import com.lazy.book.pojo.system.pojo.po.SysOperLog;
import com.lazy.book.pojo.system.pojo.vo.SysOperLogVO;
import com.lazy.book.service.system.SysOperLogService;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.PageUtils;
import com.sz.core.util.Utils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.lazy.book.pojo.system.pojo.po.table.SysOperLogTableDef.SYS_OPER_LOG;

/**
 * @author lay
 * @since 2025-09-05
 */
@Service
@RequiredArgsConstructor
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    @Override
    public void create(SysOperLogCreateDTO dto) {
        SysOperLog sysOperLog = BeanCopyUtils.copy(dto, SysOperLog.class);
        save(sysOperLog);
    }

    @Override
    public void update(SysOperLogUpdateDTO dto) {
        SysOperLog sysOperLog = BeanCopyUtils.copy(dto, SysOperLog.class);
        updateById(sysOperLog);
    }

    @Override
    public PageResult<SysOperLogVO> page(SysOperLogListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_OPER_LOG);
        if (Utils.isNotBlank(dto.getTitle())) {
            wrapper.and(SYS_OPER_LOG.TITLE.like(dto.getTitle()));
        }
        Page<SysOperLog> page = page(dto.buildPage(), wrapper);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<SysOperLogVO> list(SysOperLogListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_OPER_LOG);
        if (Utils.isNotBlank(dto.getTitle())) {
            wrapper.and(SYS_OPER_LOG.TITLE.like(dto.getTitle()));
        }
        return list(wrapper).stream().map(sysOperLog -> BeanCopyUtils.copy(sysOperLog, SysOperLogVO.class)).collect(Collectors.toList());
    }

    @Override
    public void remove(SelectIdsDTO dto) {
        CommonResponseEnum.INVALID_ID.assertIsFalse(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public SysOperLogVO detail(Object id) {
        SysOperLog sysOperLog = getById(id);
        return BeanCopyUtils.copy(sysOperLog, SysOperLogVO.class);
    }

    @Override
    public void importExcel(ImportExcelDTO dto) {

    }

    @Override
    public void exportExcel(SysOperLogListDTO dto, HttpServletResponse response) {

    }
}
